package com.zbkj.crmeb.agent.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.crmeb.agent.model.AgentAccount;
import com.zbkj.crmeb.agent.model.AgentAccountFlow;
import com.zbkj.crmeb.agent.dao.AgentAccountFlowDao;
import com.zbkj.crmeb.agent.service.AgentAccountFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.agent.service.AgentAccountService;
import com.zbkj.crmeb.chant.model.ChannelMerchantAccount;
import com.zbkj.crmeb.enums.AccountFlowTypeEnum;
import com.zbkj.crmeb.store.model.AccountFlow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 服务商资金流水表 服务实现类
 * </p>
 *
 * @author wuzp
 * @since 2022-02-15
 */
@Service
@Slf4j
public class AgentAccountFlowServiceImpl extends ServiceImpl<AgentAccountFlowDao, AgentAccountFlow> implements AgentAccountFlowService {

    @Autowired
    AgentAccountService agentAccountService;

    @Override
    public void saveAccountFlow(Integer agentId, Integer otId, Integer operateId, String operateIp, AccountFlowTypeEnum tradeType,
                                BigDecimal withdrawAccount, BigDecimal freezeAccount, String remark) {
        AgentAccount agentAccount = agentAccountService.getOne(Wrappers.<AgentAccount>lambdaQuery()
                .eq(AgentAccount::getAgentId, agentId));
        AgentAccountFlow agentAccountFlow = new AgentAccountFlow();
        agentAccountFlow.setAgentId(agentId);
        agentAccountFlow.setOtId(otId);
        agentAccountFlow.setOperateId(operateId);
        agentAccountFlow.setOperateIp(operateIp);
        agentAccountFlow.setTradeType(tradeType.getCode());
        agentAccountFlow.setWithdrawAccount(withdrawAccount);
        agentAccountFlow.setFreezeAccount(freezeAccount);
        agentAccountFlow.setRemark(remark);
        agentAccountFlow.setNowWithdrawAccount(agentAccount.getWithdrawAccount());
        agentAccountFlow.setNowFreezeAccount(agentAccount.getFreezeAccount());
        agentAccountFlow.setCreateTime(new Date());
        agentAccountFlow.setUpdateTime(new Date());
        this.save(agentAccountFlow);
        log.info("新增资金流水, 服务商: {}, JSON: {}", agentId, JSONObject.toJSONString(agentAccountFlow));
    }

}
